package cn.openea.perpartner.meily.web.controller;

import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.meily.aspect.annotation.AccessLimit;
import cn.openea.perpartner.meily.web.entity.vo.ExportDataVo;
import cn.openea.perpartner.meily.web.service.importandexport.ImportAndExportHandler;
import cn.openea.perpartner.meily.web.service.importandexport.ImportAndExportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @author ysk
 * @version 1.0.1
 * @Description 导入导出公共管理Controller
 * @createTime 2023年05月15日 16:24:00
 */
@Api(tags = "导入导出")
@RestController
@RequestMapping("")
public class ImportAndExportController {

    @Resource
    private ImportAndExportHandler importAndExportHandler;


    @AccessLimit(lockTime = 10)
    @ApiOperation("导入excel")
    @PostMapping("/common/import/excel/{action}")
    public AjaxResult importExcel(MultipartFile file, @PathVariable String action) {
        if (file == null) {
            return AjaxResult.error("导入文件不能为空");
        }
        ImportAndExportService service = importAndExportHandler.getService(action);
        return service.importExcel(file, action);
    }

    @ApiOperation("导出excel")
    @PostMapping("/common/export/excel")
    public void exportExcel(@RequestBody @Valid ExportDataVo exportDataVo, HttpServletResponse response) {
        ImportAndExportService service = importAndExportHandler.getService(exportDataVo.getActions());
        service.exportExcel(exportDataVo, response);
    }

}
